<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class main extends CI_Controller {
	
	private $facebook_config;
	
	public function __construct() {
		
		parent::__construct();
		
		$this->load->config('facebook');
		
		$this->facebook_config = array(
				'appId'  => $this->config->item('appId'),
				'secret' => $this->config->item('secret'),
		);
		
		//$this->output->enable_profiler(TRUE);
		$this->load->model('main_model');
	}
	
	public function index() {
		
		if(!$this->session->userdata('caPageView')){
		
			$this->main_model->pageview();
			$array = array(
					'caPageView'   => 'caPageView'
			);
		
			$this->session->set_userdata($array);
		}
		
		$facebook_app_id = $this->facebook_config['appId'];
		$data['facebook_app_id'] =  $facebook_app_id;
		
		$careers_view = $this->main_model->view_count('careers');
		$careers_cover_default['careers_view'] = $careers_view;
		
		$careers_view_row = $this->main_model->careers_view();
		$careers_cover_default['careers_view_row'] = $careers_view_row;
		$data['careers_cover_default'] =  $careers_cover_default;
		
		$introduction_cover_total_page = 11;
		$introduction_cover_current_page = mt_rand(1, $introduction_cover_total_page);
		
		$introduction_cover_default['current_page'] = $introduction_cover_current_page;
		$introduction_cover_default['current_page_text'] = sprintf('%02d', $introduction_cover_current_page);
		$introduction_cover_default['total_page'] = $introduction_cover_total_page;
		$data['introduction_cover_default'] =  $introduction_cover_default;
		
		$cover_rows = $this->main_model->cover();
		
		$cover_total_rows = $this->main_model->cover_total_rows();
		
		$cover_rows_array = array();
		$cover_default['main_footer_content_height'] = 770;
		foreach($cover_rows as $row) {

			$image = json_decode($row['image'], true);
			$row['image_count'] = count($image);
			if(!empty($row['image_count'])) {
				$row['image_is_image'] = $image[0]['is_image'];
				$row['image_file_name'] = $image[0]['file_name'];
				$row['image_orig_name'] = $image[0]['orig_name'];
				$row['image_image_width'] = $image[0]['image_width'];
				$row['image_image_height'] = $image[0]['image_height'];
			} else {
				$row['image_is_image'] = 0;
				$row['image_file_name'] = ';';
				$row['image_orig_name'] = '&nbsp;';
				$row['image_image_width'] = 0;
				$row['image_image_height'] = 0;
			}
			
			if($row['type'] == 'fixed' && $cover_default['main_footer_content_height'] < $row['image_image_height']) {
					$cover_default['main_footer_content_height'] = $row['image_image_height'];
			}
			
			if(empty($cover_default['font_type'])) {
				switch($row['font_color']) {
					case 'black' :
						$image_color = '';
						break;
					case 'white' :
						$image_color = '_white';
						break;
					default :
						$image_color = '';
				}
				
				$cover_default['font_type'] = $row['font_type'];
				$cover_default['font_color'] = $row['font_color'];
				$cover_default['image_color'] = $image_color;
				$cover_default['thumb_view'] = $row['thumb_view'];
				$cover_default['type'] = $row['type'];
				$cover_default['image_file_name'] = $row['image_file_name'];
			}
			
			array_push($cover_rows_array, $row);
		}
		
		$data['cover_default'] =  $cover_default;
		$data['cover_total_rows'] =  $cover_total_rows;
		$data['cover_rows'] =  $cover_rows_array;
		
		
		$main_rows = $this->main_model->main();
		
		$main_rows_array = array();
		foreach($main_rows as $row) {
			
			$row['image_is_image'] = 0;
			$row['image_file_name'] = ';';
			$row['image_orig_name'] = '&nbsp;';
			if($row['view_kind'] == 'work') {
				$work_view_row = $this->main_model->work_view($row['view_id']);
				$image = json_decode($work_view_row['image'], true);
				$row['image_count'] = count($image);
				if(!empty($row['image_count'])) {
					$row['image_is_image'] = $image[0]['is_image'];
					$row['image_file_name'] = $image[0]['file_name'];
					$row['image_orig_name'] = $image[0]['orig_name'];
				}
				$row['title'] = $work_view_row['title1'].'<br />'.$work_view_row['title2'];
			}
			else if($row['view_kind'] == 'blog') {
				$blog_view_row = $this->main_model->blog_view($row['view_id']);
				$image = json_decode($blog_view_row['image'], true);
				$row['image_count'] = count($image);
				if(!empty($row['image_count'])) {
					$row['image_is_image'] = $image[0]['is_image'];
					$row['image_file_name'] = $image[0]['file_name'];
					$row['image_orig_name'] = $image[0]['orig_name'];
				}
				$row['title'] = $blog_view_row['title'];
			}
			array_push($main_rows_array, $row);
		}
		
		$data['main_rows'] =  $main_rows_array;
		
		$this->load->view('main', $data);
	}
	
	public function work($category_id, $offset = 0) {
		
		$type = $this->input->post('type');
		$search = $this->input->post('search');
		$view_count = $this->main_model->view_count('work');
		$limit = $view_count * 4;
		
		if($type == 'all') $work_rows = $this->main_model->work($category_id, $search, $limit + $offset, 0);
		else $work_rows = $this->main_model->work($category_id, $search, $limit, $offset);
		$work_total_rows = $this->main_model->work_total_rows($category_id, $search);
		
		$work_rows_array = array();
		$works_row_count = 0;
		foreach($work_rows as $row) {
			$image = json_decode($row['image'], true);
			$row['image_count'] = count($image);
			if(!empty($row['image_count'])) {
				$row['image_is_image'] = $image[0]['is_image'];
				$row['image_file_name'] = $image[0]['file_name'];
				$row['image_orig_name'] = $image[0]['orig_name'];
			} else {
				$row['image_is_image'] = 0;
				$row['image_file_name'] = ';';
				$row['image_orig_name'] = '&nbsp;';
			}
			if($type == 'all') $row['offset'] = intval($works_row_count / $limit) * $limit;
			else $row['offset'] = $offset;
			
			array_push($work_rows_array, $row);
			$works_row_count++;
		}
		
		$data['limit'] = $limit;
		$data['offset'] = $offset + $limit;
		
		$data['work_rows'] =  $work_rows_array;
		$data['work_total_rows'] = $work_total_rows;
		
		$json = json_encode($data);
		
		$data['json'] = $json;
		
		$this->load->view('json', $data);
	}
	
	public function work_security($id) {
		$password = $this->input->post('password');
		
		$work_view_row = $this->main_model->work_view($id);
		if($work_view_row['password'] == $password) {
			$data['result'] = 1;
			
			$array = array(
				'works_view_id'   => $id
				);
			
			$this->session->set_userdata($array);
		}
		else $data['result'] = 0;
		
		$data['id'] = $id;
		
		$json = json_encode($data);
		
		$data['json'] = $json;
		
		$this->load->view('json', $data);
	}
	
	public function work_view_security($id) {
		
		$work_view_row = $this->main_model->work_view($id);
		if($work_view_row['security'] == "lock") {
			if($id == $this->session->userdata('works_view_id')) $data['result'] = 1;
			else  $data['result'] = 0;
		}
		else $data['result'] = 1;
	
		$data['id'] = $id;
		
		$json = json_encode($data);
	
		$data['json'] = $json;
	
		$this->load->view('json', $data);
	}
	
	public function work_view($id) {
		$category_id = $this->input->post('category_id');
		$search = $this->input->post('search');
		
		$work_view_row = $this->main_model->work_view($id);
		if($work_view_row['security'] == "lock") {
			if($id != $this->session->userdata('works_view_id')) exit;
		}
		
		$work_view_old = $this->main_model->work_view_old($category_id, $search, $work_view_row['sort']);
		$work_view_new = $this->main_model->work_view_new($category_id, $search, $work_view_row['sort']);
		if(empty($work_view_old)) $work_view_old['id'] = 0;
		if(empty($work_view_new)) $work_view_new['id'] = 0;
		
		$category_rows = $this->main_model->category_rows();
	
		$work_view_row['category_id'] = json_decode($work_view_row['category_id'], true);
		
		$category_title_array = array();
		foreach($work_view_row['category_id'] as $category_id_row) {
			foreach($category_rows as $row) {
				if($category_id_row == $row['id']) array_push($category_title_array, '<span data-id="'.$row['id'].'">'.$row['title'].'</span>');
			}
		}
		$category_title = implode(', ', $category_title_array);
		
		$work_view_row['category_title'] = $category_title;
		
		$tag = trim($work_view_row['tag']);
		if(!empty($tag)) {
			$tag_array = explode(',', $tag);
			$tag_html_array = array();
			foreach($tag_array as $row) {
				$row = trim($row);
				array_push($tag_html_array, '<span data-id="'.$row.'">'.$row.'</span>');
			}
			$tag = implode(', ', $tag_html_array);
		}
		$work_view_row['tag'] = $tag;
		
		if($work_view_row['date'] != '0000-00-00') $work_view_row['date'] = date('M.Y', strtotime($work_view_row['date']));
		else $work_view_row['date'] = '&nbsp;';
		
		$relation_id_array = array($work_view_row['relation_id_1'], $work_view_row['relation_id_2'], $work_view_row['relation_id_3'], $work_view_row['relation_id_4']);
		
		$relation = '';
		foreach($relation_id_array as $row) {
			if(!empty($row)) {
				$relation_work_view_row = $this->main_model->work_view($row);
				$image = json_decode($relation_work_view_row['image'], true);
				$image_count = count($image);
				if(!empty($image_count)) {
					if(!empty($image[0]['is_image'])) {
						$relation .='<li><img data-id="'.$row.'" class="works-view-relation-img" src="'.UPLOAD_DIR.'/'.$image[0]['file_name'].'" width="196px" height="110px" /></li>';
					}
				}
			}
		}
		if(!empty($relation)) $relation = '<img src="'.IMG_DIR.'/works/cs_title_also.jpg" /><ul>'.$relation.'</ul>';
		
		$work_view_row['relation'] = $relation;
		
		$data['work_view_row'] = $work_view_row;
		$data['work_view_old_id'] = $work_view_old['id'];
		$data['work_view_new_id'] = $work_view_new['id'];
	
		$json = json_encode($data);
		
		$data['json'] = $json;
		
		$this->load->view('json', $data);
	}
	
	public function work_category() {
	
		$category_rows = $this->main_model->category_rows();
	
		$data['category_rows'] = $category_rows;
	
		$json = json_encode($data);
	
		$data['json'] = $json;
	
		$this->load->view('json', $data);
	}
	
	public function blog($category_id, $offset = 0) {
	
		$type = $this->input->post('type');
		
		$view_count = $this->main_model->view_count('blog');
		$limit = $view_count;
	
		if($type == 'all') $blog_rows = $this->main_model->blog($category_id, $limit + $offset, 0);
		else $blog_rows = $this->main_model->blog($category_id, $limit, $offset);
		$blog_total_rows = $this->main_model->blog_total_rows($category_id);
	
		$blog_rows_array = array();
		$blog_row_count = 0;
		foreach($blog_rows as $row) {
			if($row['date'] != '0000-00-00') $row['date'] = date('M.Y', strtotime($row['date']));
			else $row['date'] = '&nbsp;';
			if($type == 'all') $row['offset'] = intval($blog_row_count / $limit) * $limit;
			else $row['offset'] = $offset;
			
			array_push($blog_rows_array, $row);
			$blog_row_count++;
		}
	
		$data['limit'] = $limit;
		$data['offset'] = $offset + $limit;
	
		$data['blog_rows'] =  $blog_rows_array;
		$data['blog_total_rows'] = $blog_total_rows;
	
		$json = json_encode($data);
	
		$data['json'] = $json;
	
		$this->load->view('json', $data);
	}
	
	public function blog_view($id) {
	
		$blog_view_row = $this->main_model->blog_view($id);
		
		if($blog_view_row['date'] != '0000-00-00') $blog_view_row['date'] = date('M.Y', strtotime($blog_view_row['date']));
		else $blog_view_row['date'] = '&nbsp;';
	
		$data['blog_view_row'] = $blog_view_row;
	
		$json = json_encode($data);
	
		$data['json'] = $json;
	
		$this->load->view('json', $data);
	}
	
	public function blog_category() {
	
		$category_rows = $this->main_model->blog_category_rows();
	
		$data['category_rows'] = $category_rows;
	
		$json = json_encode($data);
	
		$data['json'] = $json;
	
		$this->load->view('json', $data);
	}
	
	public function blog_archive($category_id) {
	
		$archive_rows = $this->main_model->blog_archive_rows($category_id);
	
		$data['archive_rows'] = $archive_rows;
	
		$json = json_encode($data);
	
		$data['json'] = $json;
	
		$this->load->view('json', $data);
	}
	
	public function sns_message($type, $menu, $category_id, $view_id) {
		
		switch($type) {
			case 'facebook':
				$this->load->helper('simple_html_dom_helper');
				if($menu == 'works-view') {
					$message_link = 'http://'.$_SERVER['HTTP_HOST'].'/?menu='.$menu.'&category-id='.$category_id.'&view-id='.$view_id;
						
					$work_view_row = $this->main_model->work_view($view_id);
					$message_name = $work_view_row['title1'];
					$message_description = strip_tags($work_view_row['content']);
						
					$html = str_get_html($work_view_row['content']);
					
					$message_picture = '';
					$work_view_row['image'] = json_decode($work_view_row['image'], true);
					$image_count = count($work_view_row['image']);
					if(!empty($image_count)) {
						$message_picture = 'http://'.$_SERVER['HTTP_HOST'].UPLOAD_DIR.'/'.$work_view_row['image'][0]['file_name'];
					} 
				}
				else if($menu == 'blog' || $menu == 'blog-view') {
					$message_link = 'http://'.$_SERVER['HTTP_HOST'].'/?menu=blog-view&category-id='.$category_id.'&view-id='.$view_id;
						
					$blog_view_row = $this->main_model->blog_view($view_id);
					$message_name = $blog_view_row['title'];
					$message_description = strip_tags($blog_view_row['content']);
					
					$html = str_get_html($blog_view_row['content']);
					
					$message_picture = '';
					$blog_view_row['image'] = json_decode($blog_view_row['image'], true);
					$image_count = count($blog_view_row['image']);
					if(!empty($image_count)) {
						$message_picture = 'http://'.$_SERVER['HTTP_HOST'].UPLOAD_DIR.'/'.$blog_view_row['image'][0]['file_name'];
					}
				}
				else if($menu == 'showreel') {
					$message_link = 'http://'.$_SERVER['HTTP_HOST'].'/?menu=showreel';
				
					$message_name = 'showreel';
					$message_description = 'showreel';
						
					$html = '';
					$message_picture = '';
				}
				
				$message_source = '';
				if(!empty($html)) {
					foreach($html->find('iframe') as $row) {
						if(preg_match('/http\:\/\/player.vimeo.com\/video\//i', $row->src)) {
							$vimeo_id = preg_replace('/http\:\/\/player.vimeo.com\/video\//i', '', $row->src);
							$message_source = 'http://vimeo.com/moogaloop.swf?clip_id='.$vimeo_id.'&autoplay=1';
							break;
						}
					}
				}
				
				if(empty($message_picture)) {
					$message_picture = 'http://'.$_SERVER['HTTP_HOST'].IMG_DIR.'/common/logo.png';
					if(!$message_source) {
						if(!empty($html)) {
							foreach($html->find('img') as $row) {
								if(preg_match('/http\:\/\//i', $row->src)) $message_picture = $row->src;
								else $message_picture = 'http://'.$_SERVER['HTTP_HOST'].$row->src;
								break;
							}
						}
					}
				}
				
				$message = array('method' => 'feed',
						'redirect_uri' => 'http://'.$_SERVER['HTTP_HOST'].base_url('main/facebook_scrap'),
						'picture' => $message_picture,
						'link' => $message_link,
						'name' => $message_name,
						'caption' => $_SERVER['HTTP_HOST'],
						'description' => $message_description);
				if($message_source) $message['source'] = $message_source;
				
				$json = json_encode($message);
				
				$data['json'] = $json;
				
				$this->load->view('json', $data);
				
				break;
			case 'twitter':
				$this->load->library('google_url_api');
				$this->google_url_api->enable_debug(FALSE);
				
				if($menu == 'works-view') {
					$message_link = 'http://'.$_SERVER['HTTP_HOST'].'/?menu='.$menu.'&category-id='.$category_id.'&view-id='.$view_id;
					$short_url = $this->google_url_api->shorten($message_link);
					$message_link = $short_url->id;
				
					$work_view_row = $this->main_model->work_view($view_id);
					$message_name = $work_view_row['title1'];
				}
				else if($menu == 'blog' || $menu == 'blog-view') {
					$message_link = 'http://'.$_SERVER['HTTP_HOST'].'/?menu=blog-view&category-id='.$category_id.'&view-id='.$view_id;
					$short_url = $this->google_url_api->shorten($message_link);
					$message_link = $short_url->id;
					
					$blog_view_row = $this->main_model->blog_view($view_id);
					$message_name = $blog_view_row['title'];
				}
				else if($menu == 'showreel') {
					$message_link = 'http://'.$_SERVER['HTTP_HOST'].'/?menu=showreel';
					$short_url = $this->google_url_api->shorten($message_link);
					$message_link = $short_url->id;
					
					$message_name = 'showreel';
				}
				
				$message = array('status'=> $message_name .'
						- '.$message_link);
				
				return $message;
				break;
		}
	}
	
	public function sns_login_scrap($type, $menu, $category_id, $view_id) {
		
		$result = array();
		
		switch($type) {
			case 'twitter':
				$this->load->library('twitter/tweet');
				$this->tweet->enable_debug(FALSE);
				$logged_in = $this->tweet->logged_in();
				if(!$logged_in) $result['status'] = 'no_login';
				else {
					$message = $this->sns_message($type, $menu, $category_id, $view_id);
					$this->tweet->call('post', '1/statuses/update', $message);
					$result['status'] = 'ok';
				}
				break;
		}
		
		$data = $result;
		$json = json_encode($data);
		
		$data['json'] = $json;
		
		$this->load->view('json', $data);
	}
	
	public function sns_login($type, $menu, $category_id, $view_id) {
		
		switch($type) {
			case 'twitter':
				$this->load->library('twitter/tweet');
				$this->tweet->enable_debug(FALSE);
				$logged_in = $this->tweet->logged_in();
				if(!$logged_in) {
					$array = array(
							'twitter_menu' => $menu,
							'twitter_category_id' => $category_id, 
							'twitter_view_id' => $view_id
					);
					$this->session->set_userdata($array);
					
					$this->tweet->set_callback('http://'.$_SERVER['HTTP_HOST'].base_url('main/twitter_login'));
					$this->tweet->login();
					exit;
				}
				else {
					$message = $this->sns_message($type, $menu, $category_id, $view_id);
					$this->tweet->call('post', '1/statuses/update', $message);
				}
				break;
		}
		
		$html = '
			<script>
		  	window.onload = function () {
		  		alert("Scrap!");
		  		self.close();
		 	}
		 	</script>
	 		';
		
		$data['html'] = $html;
		
		$this->load->view('html', $data);
	}
	
	public function twitter_login() {
		$type = 'twitter';
		$menu = $this->session->userdata('twitter_menu');
		$category_id = $this->session->userdata('twitter_category_id');
		$view_id = $this->session->userdata('twitter_view_id');
		
		$this->load->library('twitter/tweet');
		$this->tweet->enable_debug(FALSE);
		
		$message = $this->sns_message($type, $menu, $category_id, $view_id);
		$this->tweet->call('post', '1/statuses/update', $message);
		
		$html = '
			<script>
			window.onload = function () {
				alert("Scrap!");
				self.close();
			}
			</script>
			';
		
		$data['html'] = $html;
		
		$this->load->view('html', $data);
	}
	
	public function facebook_scrap() {
		
		$html = '
			<script>
			self.close();
			</script>
			';
		
		$data['html'] = $html;
		
		$this->load->view('html', $data);
	}
	
	public function project_request_write_action() {
		$company_name = $this->input->post('company_name');
		$user_name = $this->input->post('user_name');
		$email_address = $this->input->post('email_address');
		$phone_number = $this->input->post('phone_number');
		$film = $this->input->post('film');
		$motion_graphic = $this->input->post('motion_graphic');
		$ux = $this->input->post('ux');
		$website = $this->input->post('website');
		$etc = $this->input->post('etc');
		$budget = $this->input->post('budget');
		$schedule = $this->input->post('schedule');
		$comment = $this->input->post('comment');
		
		$kind = array(
				'film' => $film,
				'motion_graphic' => $motion_graphic,
				'ux' => $ux,
				'website' => $website,
				'etc' => $etc
				);
		
		$this->load->library('upload');
		
		$config['upload_path'] = PROJECT_REQUEST_UPLOAD_PATH;
		$config['allowed_types'] = '*';
		
		$image = array();
		$this->upload->initialize($config);
				
		$this->upload->do_upload('image');
		if(!$this->upload->display_errors()) {
			$image = $this->upload->data();
		}
		
		$this->main_model->project_request_write_action($company_name, $user_name, $email_address, $phone_number, $kind, $budget, $schedule, $comment, $image);
	}
}

/* End of file main.php */
/* Location: ./application/controllers/main.php */